// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------
// Modification history:
// feature: agent for vbs
// feature: destination order support by reception timestamp
// feature: E2E Profile04 and Configuration Function Development
// ------------------------------------------------------------------

#include <xmlparser/qos/XMLReaderQos.hpp>
#include <elog/Log.hpp>

namespace vbsutil {
namespace xmlparser {

void ReaderQos::setQos(const ReaderQos& qos, bool first_time) {
    if (first_time) {
        m_durability = qos.m_durability;
        m_durability.hasChanged = true;
    }
    if (first_time || (m_deadline.period != qos.m_deadline.period)) {
        m_deadline = qos.m_deadline;
        m_deadline.hasChanged = true;
    }
    if (first_time) {
        m_liveliness = qos.m_liveliness;
        m_liveliness.hasChanged = true;
    }
    if (first_time) {
        m_reliability = qos.m_reliability;
        m_reliability.hasChanged = true;
    }
    if (first_time) {
        m_ownership = qos.m_ownership;
        m_ownership.hasChanged = true;
    }
    if (first_time || (qos.m_partition.names() != m_partition.names())) {
        m_partition = qos.m_partition;
        m_partition.hasChanged = true;
    }

    if (first_time) {
        m_disablePositiveACKs = qos.m_disablePositiveACKs;
        m_disablePositiveACKs.hasChanged = true;
    }
    if (first_time) {
        m_e2eProtecion = qos.m_e2eProtecion;
        m_e2eProtecion.hasChanged = true;
    }
    m_ipc_mode = qos.m_ipc_mode;
    m_agent_flag = qos.m_agent_flag;
}

bool ReaderQos::canQosBeUpdated(const ReaderQos& qos) const {
    bool updatable = true;
    if (m_durability.kind != qos.m_durability.kind) {
        updatable = false;
        logDebug(RTPS_QOS_CHECK, "Durability kind cannot be changed after the creation of a subscriber.");
    }

    if (m_liveliness.kind != qos.m_liveliness.kind) {
        updatable = false;
        logDebug(RTPS_QOS_CHECK, "Liveliness Kind cannot be changed after the creation of a subscriber.");
    }

    if (m_liveliness.lease_duration != qos.m_liveliness.lease_duration) {
        updatable = false;
        logDebug(RTPS_QOS_CHECK, "Liveliness lease duration cannot be changed after the creation of a subscriber.");
    }

    if (m_liveliness.announcement_period != qos.m_liveliness.announcement_period) {
        updatable = false;
        logDebug(RTPS_QOS_CHECK, "Liveliness announcement cannot be changed after the creation of a subscriber.");
    }

    if (m_reliability.kind != qos.m_reliability.kind) {
        updatable = false;
        logDebug(RTPS_QOS_CHECK, "Reliability Kind cannot be changed after the creation of a subscriber.");
    }
    if (m_ownership.kind != qos.m_ownership.kind) {
        updatable = false;
        logDebug(RTPS_QOS_CHECK, "Ownership Kind cannot be changed after the creation of a subscriber.");
    }
    if (m_e2eProtecion.enabled != qos.m_e2eProtecion.enabled) {
        updatable = false;
        logDebug(RTPS_QOS_CHECK, "E2E protection cannot be changed after the creation of a subscriber.");
    }
    if (m_destinationOrder.kind != qos.m_destinationOrder.kind) {
        updatable = false;
        logDebug(RTPS_QOS_CHECK, "DestionationOrder kind cannot be changed after the creation of a subscriber.");
    }
    return updatable;
}

void ReaderQos::clear() {
    m_durability.clear();
    m_deadline.clear();
    m_liveliness.clear();
    m_reliability.clear();
    m_ownership.clear();
    m_partition.clear();
    m_e2eProtecion.clear();
    m_disablePositiveACKs.clear();
    m_destinationOrder.clear();
    m_ipc_mode = 0;
    m_agent_flag = 0;
}

}  // namespace xmlparser
}  // namespace vbsutil
